package
{
	import com.engine.core.controls.events.WealthEvent;
	import com.engine.core.controls.system.wealth.WealthPool;
	import com.engine.core.controls.system.wealth.WealthQueue;
	import com.engine.core.controls.system.wealth.loader.NoSandBoxLoader;
	import com.engine.core.controls.system.wealth.loader.SWFLoader;
	import com.engine.core.model.system.wealth.WealthGroupVo;
	import com.greensock.events.TransformEvent;
	import com.greensock.transform.TransformItem;
	import com.greensock.transform.TransformManager;
	import com.ming.ui.containers.Panel;
	import com.ming.ui.controls.Button;
	import com.ming.ui.controls.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.system.LoaderContext;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.utils.Dictionary;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	import org.osmf.layout.ScaleMode;
	
	public class UIEditer extends Sprite
	{
		private var queue:WealthQueue;
		private var transFormManager:TransformManager;
		private var txtX:TextField;
		private var txtY:TextField;
		private var txtWidth:TextField;
		private var txtHeight:TextField;
		private var txtName:TextField;
		private var txtClassName:TextField;
		private var xml:XML;

		private var panel:Panel;
		private var fileLoad:FileReference;
		private var fileSave:FileReference;
		private var typeStringMap:Dictionary;
		public function UIEditer()
		{
			//初始化
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			Style.getInstance().preInit();
			fileSave = new FileReference();
			fileLoad = new FileReference();
			fileLoad.addEventListener(Event.SELECT,onSelectedFileHandle);
			fileLoad.addEventListener(Event.COMPLETE,onFileLoadCompleteHandle);
			typeStringMap = new Dictionary();
			//加载素材资源
			queue=new WealthQueue();
			var group:WealthGroupVo=new WealthGroupVo();
			group.addWealth("mingUI.swf", URLLoaderDataFormat.BINARY);
			group.addWealth("viewUI.swf", URLLoaderDataFormat.BINARY);
			queue.addEventListener(WealthEvent.WEALTHGROUP_LOADED, wealthLoadedFunc);
			this.queue.put(group);
			//初始化TransformManager
			transFormManager = new TransformManager();
			transFormManager.allowDelete = true;
			transFormManager.arrowKeysMove = true;
			transFormManager.forceSelectionToFront = false;
			transFormManager.lockRotation = true;
			transFormManager.handleSize = 5;
			transFormManager.addEventListener(TransformEvent.MOVE, onMove);
			transFormManager.addEventListener(TransformEvent.SCALE, onScale);
			transFormManager.addEventListener(TransformEvent.SELECTION_CHANGE, onSelect);
			transFormManager.addEventListener(TransformEvent.DELETE, onDelete);
			
			//初始化信息显示部件
			txtName = new TextField();
			txtClassName = new TextField();
			txtX = new TextField();
			txtY = new TextField();
			txtWidth = new TextField();
			txtHeight = new TextField();
			
			addChild(txtName);
			addChild(txtClassName);
			addChild(txtX);
			addChild(txtY);
			addChild(txtWidth);
			addChild(txtHeight);
			
			txtName.y = 0;
			txtClassName.y = 20;
			txtX.y = 40;
			txtY.y =60;
			txtWidth.y = 80;
			txtHeight.y = 100;
			
			txtName.width = 300;
			txtClassName.width = 300;
			
		}
		
		protected function onDelete(e:TransformEvent):void
		{
			for each (var item:TransformItem in e.items)
			{
				transFormManager.removeItem(item);
			}
		}
		
		protected function onSelectedFileHandle(event:Event):void
		{
			while(panel.numChildren>0)
			{
				transFormManager.removeAllItems();
				panel.removeChildAt(0);
			}
			fileLoad.load();
		}
		
		protected function onFileLoadCompleteHandle(event:Event):void
		{
			xml = XML(fileLoad.data);
			if (xml)
			{
				for each (var item:XML in xml.items.item)
				{
					var dip:DisplayObject = ClassFactory.getInstance().getObjectByName(item.@className);
					dip.name = item.@name;
					typeStringMap[dip.name]=item.@className;
					panel.addChild(dip);
					transFormManager.addItem(dip, TransformManager.SCALE_WIDTH_AND_HEIGHT);
				}
				transFormManager.applyFullXML(xml,panel);
			}
		}
		
		protected function onKeyDown(event:KeyboardEvent):void
		{
			switch(event.keyCode)
			{
				case Keyboard.PAGE_UP:
					transFormManager.moveSelectionDepthUp();
					break;
				case Keyboard.PAGE_DOWN:
					transFormManager.moveSelectionDepthDown();
					break;
			}
		}
		
		protected function onScale(e:TransformEvent):void
		{
			updateInfo();
		}
		
		protected function onSelect(e:TransformEvent):void
		{
			//多选时变灰
			var num:int = transFormManager.selectedTargetObjects.length;
			if (num >1)
				transFormManager.lineColor = 0xCCCCCC;
			else
				transFormManager.lineColor = 0x3399FF;
			
			if (transFormManager.selectedTargetObjects.length>0)
			{
				updateInfo();
			}
		}
		
		protected function onMove(e:TransformEvent):void
		{
			updateInfo();
		}
		
		protected function wealthLoadedFunc(event:Event):void
		{
			panel = new Panel();
			panel.x = 300;
			panel.width = 400;
			panel.height = 400;
			addChild(panel);
			
			var btn1:Button = new Button();
			btn1.label = "保存";
			btn1.y = -50;
			addChild(btn1);
			btn1.addEventListener(MouseEvent.CLICK, 
				function ():void
				{
					xml = transFormManager.exportFullXML();
					for each (var item:XML in xml.items.item)
					{
						var str:String = item.@name;
						item.@className = typeStringMap[str];
					}
					fileSave.save(xml.toXMLString(), "test.xml");
				});
			
			var btn2:Button = new Button();
			btn2.label = "加载";
			btn2.y = -25;
			addChild(btn2);
			btn2.addEventListener(MouseEvent.CLICK, 
				function ():void
				{
					var fileFilter:FileFilter = new FileFilter("XML文档", "*.xml");
					fileLoad.browse([fileFilter]);
				});
			
			var addBtn:Button = ClassFactory.getInstance().getObjectByName("Button");
			addBtn.name = "Button";
			addChild(addBtn);
			addBtn.x = -100;
			addBtn.addEventListener(MouseEvent.CLICK, onAddBtnClick);
			
			addBtn = ClassFactory.getInstance().getObjectByName("Button");
			addBtn.name = "UIComponent1";
			addBtn.label = "UI1";
			addChild(addBtn);
			addBtn.x = -100;
			addBtn.y = 25;
			addBtn.addEventListener(MouseEvent.CLICK, onAddBtnClick);
			
			addBtn = ClassFactory.getInstance().getObjectByName("Button");
			addBtn.name = "UIComponent2";
			addBtn.label = "UI2";
			addChild(addBtn);
			addBtn.x = -100;
			addBtn.y = 50;
			addBtn.addEventListener(MouseEvent.CLICK, onAddBtnClick);
			
			addBtn = ClassFactory.getInstance().getObjectByName("Button");
			addBtn.name = "TextField";
			addBtn.label = "TextField";
			addChild(addBtn);
			addBtn.x = -100;
			addBtn.y = 75;
			addBtn.addEventListener(MouseEvent.CLICK, onAddBtnClick);
		}
		
		protected function onAddBtnClick(event:MouseEvent):void
		{
			var btn:* = ClassFactory.getInstance().getObjectByName(event.currentTarget.name);
			typeStringMap[btn.name]=event.currentTarget.name;
			panel.addChild(btn);
			transFormManager.addItem(btn,TransformManager.SCALE_WIDTH_AND_HEIGHT);
		}
		
		private function updateInfo():void
		{
			var x:int = transFormManager.selectedTargetObjects[0].x;
			var y:int = transFormManager.selectedTargetObjects[0].y;
			var width:int = transFormManager.selectedTargetObjects[0].width;
			var height:int = transFormManager.selectedTargetObjects[0].height;
			var name:String = transFormManager.selectedTargetObjects[0].name;
			var cname:String = transFormManager.selectedTargetObjects[0].name;
			
			txtX.text = "x = "+x.toString();
			txtY.text = "y = "+y.toString();
			txtWidth.text = "width = "+width.toString();
			txtHeight.text = "height = "+height.toString();
			txtName.text = "txtName = "+name;
			txtClassName.text = "className = "+typeStringMap[transFormManager.selectedTargetObjects[0].name];
		}
		
	}
}